CROSS_COMPILE=/data/public-space/toolchains/arm-none-eabi-gcc/bin/arm-none-eabi-

OBJDIR = .
SRCDIR = .
VERSION = 0.0.3
AS		= $(CROSS_COMPILE)as
LD		= $(CROSS_COMPILE)ld
CC		= $(CROSS_COMPILE)gcc
CPLUS	= $(CROSS_COMPILE)g++
CPP		= $(CC) -E
AR		= $(CROSS_COMPILE)ar
NM		= $(CROSS_COMPILE)nm
STRIP	= $(CROSS_COMPILE)strip
OBJCOPY	= $(CROSS_COMPILE)objcopy
OBJDUMP	= $(CROSS_COMPILE)objdump
RM		= rm -f
MAKEDIR	= mkdir -p


PROJECT_BUILD=release
PROJECT_DIR=.


PROJECT_NAME=F103ZT6_new
FLASH_LD=scripts/STM32F103ZEHX_FLASH.ld
STARTUP_SRC=startup_stm32f103zehx.s
PROJECT_FLAG:=-DSTM32F103xE

# PROJECT_NAME=F105VC_new
# FLASH_LD=scripts/STM32F105VCTX_FLASH.ld
# STARTUP_SRC=startup_stm32f105vctx.s
# PROJECT_FLAG:=-DSTM32F105xC

# PROJECT_NAME=F105RC_new
# STARTUP_SRC=startup_stm32f105rctx.s
# FLASH_LD=scripts/STM32F105RCTX_FLASH.ld
# PROJECT_FLAG:=-DSTM32F105xC

# PROJECT_NAME=F107VC_new
# STARTUP_SRC=startup_stm32f107vctx.s
# FLASH_LD=scripts/STM32F107VCTX_FLASH.ld
# PROJECT_FLAG:=-DSTM32F107xC

PROJECT_FLAG+=-DUSE_HAL_DRIVER 
EXECUTABLES=${PROJECT_NAME}.elf


PROJECT_SRC_FLAG+=-mcpu=cortex-m3 -std=gnu11 \
-Os -ffunction-sections -fdata-sections -Wall -fstack-usage \
--specs=nano.specs -mfloat-abi=soft -mthumb


SRC_DIR_ALL:=
PROJECT_INCLUDE:=

-include system/sub.mk
-include platform/sub.mk
-include apps/sub.mk
-include kernel/sub.mk

DRIVERS_SRC_DIR=board/STM32F1xx_HAL_Driver/Src
CORE_SRC_DIR=Core/Src
STARTUP_SRC_DIR=Core/Startup

SRC_DIR_ALL+=${DRIVERS_SRC_DIR}
SRC_DIR_ALL+=${CORE_SRC_DIR}




PROJECT_INCLUDE+=-I${PROJECT_DIR}/board/STM32F1xx_HAL_Driver/Inc/
PROJECT_INCLUDE+=-I${PROJECT_DIR}/board/CMSIS/Include/
PROJECT_INCLUDE+=-I${PROJECT_DIR}/board/CMSIS/Device/ST/STM32F1xx/Include/
PROJECT_INCLUDE+=-I${PROJECT_DIR}/Core/Inc



SRC_ALL_FILES+=$(foreach dir, $(SRC_DIR_ALL), $(wildcard $(dir)/*.c))
ASM_ALL_FILES+=$(foreach dir, $(SRC_DIR_ALL), $(wildcard $(dir)/*.s))

STARTUP_SRC_ASM= $(patsubst %.s,%.o, $(STARTUP_SRC_DIR)/${STARTUP_SRC}) 
SRC_ALL_OBJS+=$(patsubst %.c,%.o, $(SRC_ALL_FILES))
ASM_ALL_OBJS+=$(patsubst %.s,%.o, $(ASM_ALL_FILES))

BUILD_OBJS_ALL+=$(patsubst %.o,${PROJECT_BUILD}/%.o, $(SRC_ALL_OBJS))
BUILD_OBJS_ALL+=$(patsubst %.o,${PROJECT_BUILD}/%.o, $(ASM_ALL_OBJS))

all:prepare ${SRC_ALL_OBJS} ${STARTUP_SRC_ASM} ${ASM_ALL_OBJS}
	${CC} -o ${PROJECT_BUILD}/${EXECUTABLES}  ${PROJECT_BUILD}/${STARTUP_SRC_ASM} ${BUILD_OBJS_ALL}  -mcpu=cortex-m3 -T"${PROJECT_DIR}/${FLASH_LD}" --specs=nosys.specs -Wl,-Map="${PROJECT_BUILD}/${PROJECT_NAME}.map" -Wl,--gc-sections -static --specs=nano.specs -mfloat-abi=soft -mthumb -Wl,--start-group -lc -lm -Wl,--end-group
	${OBJDUMP} -h -S  ${PROJECT_BUILD}/${EXECUTABLES}  > "${PROJECT_BUILD}/${PROJECT_NAME}.list"
	@echo "Finished building:${PROJECT_BUILD}/${PROJECT_NAME}.bin.list"
	${OBJCOPY}  -O ihex  ${PROJECT_BUILD}/${EXECUTABLES}  "${PROJECT_BUILD}/${PROJECT_NAME}.hex"
	@echo "Finished building: ${PROJECT_BUILD}/${PROJECT_NAME}.bin.hex"
	${OBJCOPY}  -O binary  ${PROJECT_BUILD}/${EXECUTABLES}  "${PROJECT_BUILD}/${PROJECT_NAME}.bin"
	@echo "Finished building: ${PROJECT_BUILD}/${PROJECT_NAME}.bin"

prepare:
	mkdir ${PROJECT_BUILD} -p
	mkdir -p $(foreach dir, $(SRC_DIR_ALL), ${PROJECT_BUILD}/${dir})
	mkdir -p ${PROJECT_BUILD}/${STARTUP_SRC_DIR}
	@echo ${STARTUP_SRC_ASM}
	@echo ${SRC_ALL_OBJS}
	@echo ${SRC_ALL_FILES}
	@echo ${ASM_ALL_OBJS}


${SRC_ALL_OBJS}:%.o:%.c
	${CC} $< -c ${PROJECT_FLAG} ${PROJECT_SRC_FLAG} ${PROJECT_INCLUDE}  -MMD -MP -MF"${PROJECT_BUILD}/$(@:%.o=%.d)" -MT"${PROJECT_BUILD}/$@"  -o "${PROJECT_BUILD}/$@"
	echo  "${PROJECT_BUILD}/$@" >>${PROJECT_BUILD}/objects.list	

${ASM_ALL_OBJS}:%.o:%.s
	${CC} -mcpu=cortex-m3 -c -x assembler-with-cpp -MMD -MP -MF"${PROJECT_BUILD}/$(@:%.o=%.d)" -MT"${PROJECT_BUILD}/$@" --specs=nano.specs -mfloat-abi=soft -mthumb -o "${PROJECT_BUILD}/$@" "$<"
	echo  "${PROJECT_BUILD}/$@" >>${PROJECT_BUILD}/objects.list	

${STARTUP_SRC_ASM}:%.o:%.s
	${CC} -mcpu=cortex-m3 -c -x assembler-with-cpp -MMD -MP -MF"${PROJECT_BUILD}/$(@:%.o=%.d)" -MT"${PROJECT_BUILD}/$@" --specs=nano.specs -mfloat-abi=soft -mthumb -o "${PROJECT_BUILD}/$@" "$<"
	echo  "${PROJECT_BUILD}/$@" >>${PROJECT_BUILD}/objects.list	

clean:
	rm ${PROJECT_BUILD} -rf

.PHONY:all prepare clean
